/*
* @author : TF-BJ-C064
* @creation : 2014-6-27 下午4:30:37
* @description : 
*
*/

package com.action.park;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bean.Money;
import com.bean.ParkAccountRecord;
import com.bean.ParkOrder;
import com.dao.ParkOrderDao;
import com.service.OrderStatisticsService;

import com.tool.DateTimeTools;
import com.tool.RegexTool;
import com.util.GUtil;

public class MyOrderStatisticsAction extends BaseParkAction{

	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	private static String Tag = MyPayStatisticsAction.class.getName();
	 
	OrderStatisticsService service;
	ParkOrder parkOrder;
	List<ParkOrder> list;
	Boolean queryMyOrder=true;//true: 收费员查询自己的订单 ; false查询所有订单
	Timestamp t;
	String stattype; // day, week, month, year
	String datetime; // "yyyy-MM-dd", "yyyy-MM-dd", "yyyy-MM", "yyyy"
	JSONObject json;

	@Override
	public String handle() throws Exception {
		this.setHeaderTitle("我的订单统计");
		return GUtil.SUCCESS;
	}

	/**
	 * @return  绩效统计
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("我的订单统计");
		log.info("MyOrderStatistics");
		boolean bPass = true;
		if(stattype==null){
			bPass = false;
			this.setMsg("类型错误"); 
		}
		if(datetime==null){
			bPass = false;
			this.setMsg("时间类型错误"); 
		}
		if(bPass){
			if(stattype.equals("day")){
				return this.queryByDay();
			}else if(stattype.equals("week")){
				return this.queryByWeek();
			}else if(stattype.equals("month")){
				return this.queryByMonth();
			}else if(stattype.equals("year")){
				return this.queryByYear();
			}
			this.setMsg("参数错误  ");
		}

		PrintWriter out = response.getWriter();
		out.print( this.getMsg());
		out.close();

		return GUtil.NONE;
	}
	
	/**
	 * 按天查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByDay() throws Exception{
		String daytime = datetime.substring(0,10).trim();
		log.info(Tag+".queryByDay() daytime="+daytime);
		List<ParkOrder> payInOut = new ArrayList<ParkOrder>();
		List<Money> moneyList = new ArrayList<Money>();

		json = new JSONObject();

		if(RegexTool.checkDateTime(daytime)){
			String ret = service.queryByDay(payInOut, moneyList, daytime, user.getId());//
			if(ret.endsWith(GUtil.SUCCESS)){
				json.put("error", 0);
				json.put("msg", "操作成功");
				json.put("payInOut", JSONArray.fromObject( payInOut ) );
				json.put("moneyList", JSONArray.fromObject( moneyList ) );
			}else{
				json.put("error", -1);
				json.put("msg", "操作失败 "+ret);
			}
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 日期格式错误");
		}
		
		log.info(Tag+" "+json.toString());
		
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.close();

		return GUtil.NONE;
		
	}
	/**
	 * 按周查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByWeek() throws Exception{
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkDateTime(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//String datetime = "2014-01-08";
		Date timelong = formatDate.parse(datetime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getWeekDays(inTime, outStartTime, outEndTime);

		List<ParkOrder> payInOut = new ArrayList<ParkOrder>();//收支统计
		List<Money> moneyList = new ArrayList<Money>();//收支统计(根据类型统计)
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();

		json = new JSONObject();
		Long[][] data = service.queryByWeek(payInOut, moneyList, paylistInOutByDay, datetime, user.getId());
		if(data!=null){
			log.info("data1.lenght="+data.length);
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ) );
			json.put("moneyList", JSONArray.fromObject( moneyList ) );
			//check 
			if(data.length!=5){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data[0].length);
			}
			json.put("data", data);
			
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();

		return GUtil.NONE;
	}
	/**
	 * 按月查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByMonth() throws Exception{
		datetime =datetime.substring(0,7);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkMonth(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
		//String datetime = "2014-05";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		List<ParkOrder> payInOut = new ArrayList<ParkOrder>();
		List<Money> moneyList = new ArrayList<Money>();
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();

		json = new JSONObject();
		Long[][] data2 = service.queryByMonth(payInOut, moneyList, paylistInOutByDay, datetime, user.getId());
		if(data2!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ) );
			json.put("moneyList", JSONArray.fromObject( moneyList ) );
			//check 
			if(data2.length>5){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data2[0].length);
			}
			json.put("data2", data2);
			
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();
		
		return GUtil.NONE;
	}
	/**
	 * 按年查询订单数量
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @return json
	 * @throws Exception 
	 */
	public String queryByYear() throws Exception{
		datetime =datetime.substring(0,4);
		PrintWriter out = response.getWriter();
		if( !RegexTool.checkYear(datetime) ){
			log.info("输入日期错误 datetime="+datetime);
			out.print("输入日期错误"+datetime);
			out.close();
			return GUtil.NONE;
		}
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy");
		//String datetime = "2014";
		Date timelong = formatDate.parse(datetime);// for test it is the right time, otherwise throw ParseException
		
		List<ParkOrder> payInOut = new ArrayList<ParkOrder>();
		List<Money> moneyList = new ArrayList<Money>();
		Map<Double, Double> paylistInOutByDay = new HashMap<Double, Double>();

		json = new JSONObject();
		Double[][] data = service.queryByYear(payInOut, moneyList, paylistInOutByDay, datetime, user.getId());
		if(data!=null){
			json.put("error", 0);
			json.put("msg", "操作成功");
			json.put("payInOut", JSONArray.fromObject( payInOut ) );
			json.put("moneyList", JSONArray.fromObject( moneyList ) );
			//check 
			if(data.length>4){
				json.put("error", -2);
				log.error(Tag+" 系统错误 queryByMonth");
				json.put("msg", "系统错误 ");
			}else{
				json.put("sum", data[0].length);
			}
			json.put("data", data);
		}else{
			json.put("error", -1);
			json.put("msg", "操作失败 ");
		}
		out.print(json);
		out.close();
		
		return GUtil.NONE;
	}
	@Override
	public String delete() throws Exception {
		return null;
	}
	@Override
	public String view() throws Exception {
		if(id==null)
			return GUtil.ERROR;
		parkOrder =(ParkOrder) service.get(Long.valueOf(id));
		return GUtil.VIEW;
	}

	@Override
	public String add() throws Exception {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}
	/*
	 * 导出excel
	 */
	public String export() throws Exception {
		
		return null;
	}
	/**
	 * @return the parkOrder
	 */
	public ParkOrder getParkOrder() {
		return parkOrder;
	}

	/**
	 * @param parkOrder the parkOrder to set
	 */
	public void setParkOrder(ParkOrder parkOrder) {
		this.parkOrder = parkOrder;
	}

	/**
	 * @return the list
	 */
	public List<ParkOrder> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ParkOrder> list) {
		this.list = list;
	}

	/**
	 * @return the queryMyOrder
	 */
	public Boolean getQueryMyOrder() {
		return queryMyOrder;
	}

	/**
	 * @param queryMyOrder the queryMyOrder to set
	 */
	public void setQueryMyOrder(Boolean queryMyOrder) {
		this.queryMyOrder = queryMyOrder;
	}

	public Timestamp getT() {
		return t;
	}

	public void setT(Timestamp t) {
		this.t = t;
	}

	public OrderStatisticsService getService() {
		return service;
	}

	public void setService(OrderStatisticsService service) {
		this.service = service;
	}

	public String getStattype() {
		return stattype;
	}

	public void setStattype(String stattype) {
		this.stattype = stattype;
	}

	public String getDatetime() {
		return datetime;
	}

	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

}
